<template>
  <div class="flex-row auto"  style="margin-top:10px;width:96%" >
    <form class="form-horizontal" style="flex: 1;">
      <div class="form-group auto">
        <label for="quantityupper"   class="col-sm-2 control-label">气量充值上限:</label>
         <div class="col-sm-2"  >
           <input type="number" class="form-control" v-model="model.settings.quantityupper"  >
         </div>
         <label for="quantitylower"   class="col-sm-2 control-label">气量充值下限 :</label>
         <div class="col-sm-2" >
           <input type="number" class="form-control" v-model="model.settings.quantitylower"  >
         </div>
         <label for="overquantity"   class="col-sm-2 control-label">气量透支限额:</label>
         <div class="col-sm-2" >
           <input type="number" class="form-control" v-model="model.settings.overdraftquantity" >
         </div>
       </div>

       <div class="form-group auto" style="margin-top:10px;">
         <label for="warnquantity1"   class="col-sm-2 control-label">第一报警气量:</label>
           <div class="col-sm-2" >
             <input type="number" class="form-control" v-model="model.settings.warnquantity1" >
           </div>
           <label for="warnquantity2"   class="col-sm-2 control-label">第二报警气量:</label>
           <div class="col-sm-2" >
             <input type="number" class="form-control" v-model="model.settings.warnquantity2"  >
           </div>
           <label for="amountupper"   class="col-sm-2 control-label">金额充值上限:</label>
           <div class="col-sm-2">
             <input type="number" class="form-control" v-model="model.settings.amountupper"  >
           </div>
       </div>
       <div class="form-group auto" style="margin-top:10px;">
         <label for="amountlowe"   class="col-sm-2 control-label">金额充值下限:</label>
           <div class="col-sm-2" >
             <input type="number" class="form-control" v-model="model.settings.amountlower" >
           </div>
           <label for="overamount"   class="col-sm-2 control-label">金额透支限额:</label>
           <div class="col-sm-2" >
             <input type="number" class="form-control" v-model="model.settings.overdraftamount"  >
           </div>
           <label for="warnamount1"  class="col-sm-2 control-label">第一报警金额:</label>
           <div class="col-sm-2">
             <input type="number" class="form-control" v-model="model.settings.warnamount1"  >
           </div>
       </div>
       <div class="form-group auto" style="margin-top:10px;">
         <label for="warnamount2"   class="col-sm-2 control-label">第二报警金额:</label>
           <div class="col-sm-2" >
             <input type="number" class="form-control" v-model="model.settings.warnamount2" >
           </div>
           <label for="timedeviation "   class="col-sm-2 control-label">校时允许偏差:</label>
           <div class="col-sm-2" >
             <input type="number" class="form-control" v-model="model.settings.timedeviation " >
           </div>
           <label for="dstime3"   class="col-sm-2 control-label">调试选项:</label>
           <div class="col-sm-2">
             <v-select :value.sync="model.settings.debugoption" :options='debugOption' placeholder='调试选项' close-on-select>
             </v-select>
           </div>
       </div>
    </form>
  </div>
</template>

<script>
export default {
  data () {
    return {
      Settingmodel: {},
      model: {
        meterno: '',
        settingtype: 1,
        settings: {debugoption: []}
      }
    }
  },
  props: ['data'],
  ready () {
    if (this.data) {
      this.model.meterno = this.data.f_meternumber
      this.getparam(this.data.f_user_id)
    }
  },
  watch: {
    'data' (val) {
      if (val) {
        this.model.meterno = val.f_meternumber
        this.getparam(val.f_user_id)
      }
    }
  },
  methods: {
    getparam (f_user_id) {
      let tablename = 't_wmruntimesetting'
      let condition = 'f_user_id=' + "'" + f_user_id + "'"
      this.$SqlService.singleTable(tablename, condition)
      .then(
        (res) => {
          if (res.data.length === 1) {
            this.model.settings = this.reciveparams(res.data[0])
            this.model.settings.debugoption[0] = res.data[0].f_debugoption
            this.Settingmodel.id = res.data[0].id
          }
        }
      ).catch(
        (error) => this.$dispatch('error', '运行参数设置', this.data, error)
      )
    },
    changeTwoDecimal (number) {
      var firstnumber = Math.round(number * 100) / 100
      var secondnumber = firstnumber.toString()
      var pos_decimal = secondnumber.indexOf('.')
      if (pos_decimal < 0) {
        pos_decimal = secondnumber.length
        secondnumber += '.'
      }
      for (var i = secondnumber.length; i <= pos_decimal + 2; i++) {
        secondnumber += '0'
      }
      return secondnumber
    },
    converseparam () {
      this.model.settings.quantityupper = this.changeTwoDecimal(this.model.settings.quantityupper)
      this.model.settings.quantitylower = this.changeTwoDecimal(this.model.settings.quantitylower)
      this.model.settings.overdraftquantity = this.changeTwoDecimal(this.model.settings.overdraftquantity)
      this.model.settings.warnquantity1 = this.changeTwoDecimal(this.model.settings.warnquantity1)
      this.model.settings.warnquantity2 = this.changeTwoDecimal(this.model.settings.warnquantity2)
      this.model.settings.amountupper = this.changeTwoDecimal(this.model.settings.amountupper)
      this.model.settings.amountlower = this.changeTwoDecimal(this.model.settings.amountlower)
      this.model.settings.overdraftamount = this.changeTwoDecimal(this.model.settings.overdraftamount)
      this.model.settings.warnamount1 = this.changeTwoDecimal(this.model.settings.warnamount1)
      this.model.settings.warnamount2 = this.changeTwoDecimal(this.model.settings.warnamount2)
      this.model.settings.debugoption = this.model.settings.debugoption[0]
      this.Settingmodel = Object.assign({}, this.Settingmodel, this.getSettingmodel())
    },
    getSettingmodel () {
      return {
        f_amountupper: this.model.settings.amountupper,
        f_quantityupper: this.model.settings.quantityupper,
        f_quantitylower: this.model.settings.quantitylower,
        f_overdraftquantity: this.model.settings.overdraftquantity,
        f_warnquantity1: this.model.settings.warnquantity1,
        f_warnquantity2: this.model.settings.amountupper,
        f_amountlower: this.model.settings.amountlower,
        f_overdraftamount: this.model.settings.overdraftamount,
        f_warnamount1: this.model.settings.warnamount1,
        f_warnamount2: this.model.settings.warnamount2,
        f_timedeviation: this.model.settings.timedeviation,
        f_debugoption: this.model.settings.debugoption[0]
      }
    },
    reciveparams (val) {
      return {
        quantityupper: val.f_quantityupper,
        quantitylower: val.f_quantitylower,
        overdraftquantity: val.f_overdraftquantity,
        warnquantity1: val.f_warnquantity1,
        warnquantity2: val.f_warnquantity2,
        amountupper: val.f_amountupper,
        amountlower: val.f_amountlower,
        overdraftamount: val.f_overdraftamount,
        warnamount1: val.f_warnamount1,
        warnamount2: val.f_warnamount2,
        timedeviation: val.f_timedeviation,
        debugoption: []
      }
    }
  },
  computed: {
    debugOption () {
      return [{label: '关闭', value: '0'},
              {label: '开启', value: '1'},
              {label: '生产调试', value: '2'}
      ]
    }
  }
}
</script>
